package com.java.test.interview.minshengyidai;

import org.junit.Test;

/**
 * @author TonyOne
 * @version 1.0
 * @desc
 * @date 2018/10/16 9:42
 * @company
 */
public class JavaEngineerInterview {

    @Test
    public void equalsInteger(){
        Integer a = 100;
        Integer b = 100;
        System.out.println(a==b);
        System.out.println(a.equals(b));
        System.out.println(a.equals(100));
    }

    @Test
    public void equalsInteger2(){
        Integer a = new Integer(100);
        Integer b = new Integer(100);
        System.out.println(a==b);
        System.out.println(a.equals(b));
        System.out.println(a.equals(100));
    }

    @Test
    public void equalsShort(){
        Short a = 100;
        Short b = 100;
        System.out.println(a==b);
        System.out.println(a.equals(b));
        System.out.println(a.equals(100));
    }

    @Test
    public void stringEquals(){
        String str1 = "hello";
        String str2 = "he" + new String("llo");
        System.out.println(str1==str2);
        System.out.println(str1.equals(str2));
    }

    @Test
    public void testContinue1(){
        System.out.println("--------------测试continue1---------");
        for(int i =1; i<=5; i++) {
            if(i==3) continue;
            System.out.println("i="+i);
        }
    }

    @Test
    public void testBreak1(){
        System.out.println("--------------测试break1---------");
        for(int i =1; i<=5; i++) {
            if(i==3) break;
            System.out.println("i="+i);
        }
    }

    @Test
    public void testBreak2(){
        System.out.println("--------------测试break2---------");
        int i=1;
        int k=4;
        lable:
        for(; i<=5; i++,k--) {
            if(k==0) break lable;
            System.out.println("i="+i+"; k="+k);
        }
    }

    @Test
    public void testContinue2(){
        System.out.println("--------------测试continue2---------");
        lable1:
        for(int i=1; i < 10; i++) {
            label2:
            System.out.println();
            System.out.println("i="+i);
            for(int j =0 ; j<10;j++) {
                System.out.print("j="+j+" ");
                if(j==8) continue lable1;
            }
            System.out.println();
        }
    }

    /**
     * 存在使i + 1 < i的数吗（）
     * 解析：如果i为int型，那么当i为int能表示的最大整数时，i+1就溢出变成负数了，此时不就<i
     */
    @Test
    public void testComplement() {
        int i = 2147483647;
        System.out.println(i+1<i);

        long l = Long.MAX_VALUE;
        System.out.println(l+1<l);
    }

    /**
     * 是否存在数i  ,使得 i -  1 > i
     */
    @Test
    public void testComplement2() {
        int i = Integer.MIN_VALUE;
        System.out.println(i-1>i);
    }

    /**
     * 是否存在数i  ,使得 i + 1 = i
     * 这个问题参考极限值，当max值趋近正无穷时，Double max = Double.MAX_VALUE;
     */
    @Test
    public void testExtremity() {
        Double maxD = Double.MAX_VALUE;
        System.out.println(maxD+1==maxD);
        System.out.println(maxD+100000000==maxD);
        System.out.println(maxD-100000000==maxD);

        Float maxF = Float.MAX_VALUE;
        System.out.println(maxF+1==maxF);
        System.out.println(maxF+100000000==maxF);
        System.out.println(maxF-100000000==maxF);
    }

    /**
     * 扩展：存在使i > j || i <= j不成立的数吗（）
     * 答案：存在
     * 解析：比如Double.NaN或Float.NaN
     */
    @Test
    public void testNan() {
        double d1 = Double.NaN;
        double d2 = Double.NaN;
        System.out.println(d1==d2);
        System.out.println(d1>d2||d1<=d2);

        float f1 = Float.NaN;
        float f2 = Float.NaN;
        System.out.println(f1==f2);
        System.out.println(f1>f2||f1<=f2);
    }


}
